﻿using System;
using System.Data;
using SQLPXBase;

namespace GianosNET.Common.Classes
{
    public class sqlStatus : SQLProxyBase
    {
        public sqlStatus(MSSQLServerConnectionString connStr)
            : base(connStr)
        {

        }

        public void CleanStatus()
        {
            ProxyCommand cmd = CreateCommandForStoredProcedure("CleanStatus");
            ExecuteNoQuery(cmd);
        }

        public void NotifyRootStatus(string status)
        {
            ProxyCommand cmd = CreateCommandForStoredProcedure("NotifyRootStatus");
            cmd.AddParameters(status);
            ExecuteNoQuery(cmd);
        }

        public void NotifyServiceStatus(string svcName, string status)
        {
            ProxyCommand cmd = CreateCommandForStoredProcedure("NotifyServiceStatus");
            cmd.AddParameters(svcName, status);
            ExecuteNoQuery(cmd);
        }

        public Counter GetCounter(string svcName, string instanceName = "")
        {
            ProxyCommand cmd = CreateCommandForStoredProcedure("GetCounter");
            cmd.AddParameters(svcName, instanceName);
            DataRow rw = ExecuteSingleResultOrNothing(cmd);

            if (rw == null)
                return new Counter(svcName, instanceName);
            else
            {
                int? counter = rw["lastCounter"] as int?;
                DateTime? date = rw["lastDate"] as DateTime?;
                string st = rw["lastString"] as string;

                return new Counter(svcName, instanceName, counter, date, st);
            }
        }

        public void SetCounter(Counter c)
        {
            ProxyCommand cmd = CreateCommandForStoredProcedure("SetCounter");
            cmd.AddParameters(c.ServiceName, c.InstanceName, c.LastCounter, c.LastDate, c.LastString);

            ExecuteNoQuery(cmd);
        }
    }
}